/**
 * 
 */
package com.eshopper.user.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.eshopper.user.model.User;
import com.eshopper.user.repository.jpa.UserRepository;
import com.eshopper.user.security.SecurityUser;

/**
 * @author Arun Kumar, Nov 27, 2014
 *
 */
@Component("customUserDetailService")
public class CustomUserDetailService implements UserDetailsService {

	private static final Logger LOGGER = LoggerFactory.getLogger(CustomUserDetailService.class);

	@Resource
	private UserRepository repository;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetailsService#
	 * loadUserByUsername(java.lang.String)
	 */

	public UserDetails loadUserByUsername(String emailAddress)
			throws UsernameNotFoundException {

		User user = repository.findUserByEmail(emailAddress);

		LOGGER.info("CustomUserDetailService : {}", user);
		
		return new SecurityUser(user);
	}

}
